package com.aabte.openfeignlearn.example.consumer;

import com.aabte.openfeignlearn.example.interfaces.HelloService;
import com.aabte.openfeignlearn.example.interfaces.Person;
import com.aabte.openfeignlearn.example.interfaces.ResponseBean;
import com.aabte.openfeignlearn.example.interfaces.ServletUtils;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Daniel
 */
@Slf4j
@RestController
@RequestMapping(value = "/test")
public class ConsumerHelloController {

    @Resource
    private HelloService helloService;

    @GetMapping(value = "/200")
    public ResponseBean<ResponseBean<Person>> test200() {
        ResponseBean<Person> result = helloService.hello200();
        return ResponseBean.<ResponseBean<Person>>builder().code("0").message("success").data(result).build();
    }
    @GetMapping(value = "/400")
    public ResponseBean<ResponseBean<Person>> test400() {
        try {
            ResponseBean<Person> result = helloService.hello400();
            return ResponseBean.<ResponseBean<Person>>builder().code("0").message("success").data(result).build();
        } catch (FeignException e) {
            log.error("", e);
            ServletUtils.setStatus(e.status());
            return ResponseBean.<ResponseBean<Person>>builder().code(String.valueOf(e.status())).message(e.contentUTF8()).build();
        }
    }
    @GetMapping(value = "/404")
    public ResponseBean<ResponseBean<Person>> test404() {
        try {
            ResponseBean<Person> result = helloService.hello404();
            return ResponseBean.<ResponseBean<Person>>builder().code("0").message("success").data(result).build();
        } catch (FeignException e) {
            log.error("", e);
            ServletUtils.setStatus(e.status());
            return ResponseBean.<ResponseBean<Person>>builder().code(String.valueOf(e.status())).message(e.contentUTF8()).build();
        }
    }
    @GetMapping(value = "/500")
    public ResponseBean<ResponseBean<Person>> test500() {
        try {
            ResponseBean<Person> result = helloService.hello500();
            return ResponseBean.<ResponseBean<Person>>builder().code("0").message("success").data(result).build();
        } catch (FeignException e) {
            log.error("", e);
            ServletUtils.setStatus(e.status());
            return ResponseBean.<ResponseBean<Person>>builder().code(String.valueOf(e.status())).message(e.contentUTF8()).build();
        }
    }

}
